package org.hydrakyoufeng.stuplatform.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.hydrakyoufeng.stuplatform.dto.AdminLoginDTO;
import org.hydrakyoufeng.stuplatform.dto.UserDTO;
import org.hydrakyoufeng.stuplatform.dto.UserLoginDTO;
import org.hydrakyoufeng.stuplatform.service.LoginService;
import org.hydrakyoufeng.stuplatform.validate.UserGetMobileCode;
import org.hydrakyoufeng.stuplatform.validate.UserLoginCode;
import org.hydrakyoufeng.stuplatform.validate.UserLoginPassword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("login")
@Tag(name = "登录")
public class LoginController {

    private final LoginService loginService;

    @Autowired
    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }



    /**
     * 管理员登录
     * Valid注解：进行参数校验
     * RequestBody注解：将请求体参数绑定到方法参数上
     * @param loginDTO
     * @return
     */
    @Validated
    @PostMapping("adminLogin")
    @Operation(summary="管理员登录",description = "管理员登录接口")
    public ResponseEntity adminLogin(@Valid @RequestBody AdminLoginDTO loginDTO, HttpServletRequest request) throws Exception {
        return ResponseEntity.ok().body(loginService.adminLogin(loginDTO,request));
    }

    /**
     * 用户注册：用户自己在前端填写信息进行注册
     * @param userDTO
     * @return
     */
    @Validated
    @PostMapping("userLogon")
    @Operation(summary="用户注册",description = "用户注册接口")
    public ResponseEntity userLogon(@Valid @RequestBody UserDTO userDTO) throws Exception {
        return ResponseEntity.ok().body(loginService.userLogon(userDTO));
    }

    /**
     * 用于注册是判断是手机号码是否存在。
     * ResultEntity中的data表示根据电话号码查询出的用户数据，如果大于等于1表示手机号码已经存在。
     * @param mobile
     * @return
     * @throws Exception
     */
    @GetMapping("getExistsMobile/{mobile}")
    public ResponseEntity getExistsMobile(@PathVariable("mobile") String mobile) throws Exception {
        return ResponseEntity.ok(loginService.queryUserCount(mobile));
    }

    /**
     * 获取图形验证码
     * @return
     * @throws Exception
     */
    @GetMapping("verificationCode")
    public ResponseEntity verificationCode() throws Exception {
        return ResponseEntity.ok(loginService.verificationCode());
    }


    /**
     * 用户使用密码登录。
     * 注意：要使用分组校验时需要用到Validated注解
     * @param userLoginDTO
     * @return
     * @throws Exception
     */
    @PostMapping("userLoginByPassword")
    public ResponseEntity userLoginByPassword(@Validated(UserLoginPassword.class) @RequestBody UserLoginDTO userLoginDTO,
                                          HttpServletRequest request) throws Exception {
        return ResponseEntity.ok(loginService.userLoginByPassword(userLoginDTO,request));
    }

    /**
     * 发送手机验证码
     * @return
     * @throws Exception
     */
    @PostMapping("sendMsgCode")
    public ResponseEntity sendMsgCode(@Validated(UserGetMobileCode.class) @RequestBody UserLoginDTO userLoginDT) throws Exception {
        return ResponseEntity.ok(loginService.sendMsgCode(userLoginDT));
    }

    /**
     * 用户使用验证码登录。
     * 注意：要使用分组校验时需要用到Validated注解
     * @param userLoginDTO
     * @return
     * @throws Exception
     */
    @PostMapping("userLoginByCode")
    public ResponseEntity userLoginByCode(@Validated(UserLoginCode.class) @RequestBody UserLoginDTO userLoginDTO,
                                              HttpServletRequest request) throws Exception {
        return ResponseEntity.ok(loginService.userLoginByCode(userLoginDTO,request));
    }

    /**
     * 用户登出
     * @return
     * @throws Exception
     */
    @GetMapping("userLogout")
    public ResponseEntity userLogout(HttpServletRequest request) throws Exception {
        return ResponseEntity.ok(loginService.userLogout(request));
    }

}
